﻿using System;
using System.Collections.Generic;
using System.Linq;
using EvincePayroll.Data;
using System.Configuration;
using EvincePayroll.Utility;
namespace EvincePayroll.Communication.Database
{
    public class DatabaseRepository : IDatabaseRepository, IDisposable
    {
        EvincePayrollDataContext db = new EvincePayrollDataContext(ConfigurationManager.ConnectionStrings["EvincePayrollSQLConnectionString"].ToString());

        /// <summary>
        ///  Employee related interfaces
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        /// 

      

        public void SendPasswordMail(string emailId, string message)
        {

            var toAddress = emailId;

            string subject = "Forgotten password";
            string body = "From: Admin \n";
            body += message;
            MailSending mail = new MailSending();
            mail.SendMail(toAddress, subject, body);

        }
     

        public string SendMailMethod(string emailId, string purpose, string subject, string message)
        {
            var toAddress = emailId;
            string body = "From: Admin \n";
            body += message;
            MailSending mail = new MailSending();
            mail.SendMail(toAddress, subject, body);
            return "";

        }

      


        /// <summary>
        /// New Methods for communication(CRUD operations)
        /// </summary>
        /// 
        #region CRUD operation for AppOption

        public bool CreateAppOption(AppOptionData appOptionData)
        {
            bool result = false;
            if (appOptionData != null)
            {
                AppOption appOption = new AppOption();
                try
                {
                    appOption.ParamId = Guid.NewGuid();
                    appOption.ParamKey = appOptionData.ParamKey;
                    appOption.ParamValue = appOptionData.ParamValue;
                    appOption.DisplaySequence = appOptionData.DispalySequence;
                    appOption.IsActive = Convert.ToBoolean(appOptionData.IsActive);

                    db.AppOptions.InsertOnSubmit(appOption);
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public List<AppOptionData> GetAppOptionDataList()
        {
            List<AppOptionData> result = null;
            try
            {
                result = (from info in db.AppOptions
                          select new AppOptionData
                          {
                              ParamId = info.ParamId,
                              ParamKey = info.ParamKey,
                              ParamValue = info.ParamValue,
                              DispalySequence = info.DisplaySequence,
                              IsActive = (byte)info.DisplaySequence
                          }).ToList<AppOptionData>();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }
            return result;
        }

        public bool DeleteAppOption(string paramId)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(paramId))
            {
                try
                {
                    var appOption = db.AppOptions.Where(x => x.ParamId.Equals(paramId)).FirstOrDefault();
                    db.AppOptions.DeleteOnSubmit(appOption);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public bool UpdateAppOption(AppOptionData appOptionData)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(appOptionData.ParamId.ToString()))
            {
                try
                {
                    AppOption appOption = db.AppOptions.Where(x => x.ParamId.Equals(appOptionData.ParamId)).FirstOrDefault();
                    appOption.ParamKey = appOptionData.ParamKey;
                    appOption.ParamValue = appOptionData.ParamValue;
                    appOption.DisplaySequence = appOptionData.DispalySequence;
                    appOption.IsActive = Convert.ToBoolean(appOptionData.IsActive);
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public AppOptionData GetAppOptionDataByParamId(string paramId)
        {
            AppOptionData result = null;
            if (!string.IsNullOrEmpty(paramId))
            {
                try
                {
                    result = (from info in db.AppOptions
                              where info.ParamId.ToString() == paramId
                              select new AppOptionData
                              {
                                  ParamId = info.ParamId,
                                  ParamKey = info.ParamKey,
                                  ParamValue = info.ParamValue,
                                  DispalySequence = info.DisplaySequence,
                                  //IsActive = info.IsActive
                              }).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        #endregion

        #region CRUD operation for Attendance

        public bool CreateAttendance(AttendanceData attendanceData)
        {
            bool result = false;
            if (attendanceData != null)
            {
                Attendance attendance = new Attendance();
                try
                {
                    attendance.AttendanceId = Guid.NewGuid();
                    attendance.EmployeeId = attendanceData.EmployeeId;
                    attendance.ArrivingTime = attendanceData.ArrivingTime;
                    attendance.LeavingTime = attendanceData.LeavingTime;
                    attendance.Date = attendanceData.Date;
                    attendance.Status = attendanceData.Status;

                    db.Attendances.InsertOnSubmit(attendance);
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public List<AttendanceData> GetAttendanceDataList()
        {
            List<AttendanceData> result = null;
            try
            {
                result = (from info in db.Attendances
                          select new AttendanceData
                          {
                              AttendanceId = info.AttendanceId,
                              EmployeeId = info.EmployeeId,
                              ArrivingTime = (DateTime)info.ArrivingTime,
                              LeavingTime = (DateTime)info.LeavingTime,
                              Date = (DateTime)info.Date,
                              Status = info.Status
                          }).ToList<AttendanceData>();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }
            return result;
        }

        public bool DeleteAttendance(string attendanceId)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(attendanceId))
            {
                try
                {
                    var attendance = db.Attendances.Where(x => x.AttendanceId.Equals(attendanceId)).FirstOrDefault();
                    db.Attendances.DeleteOnSubmit(attendance);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public bool UpdateAttendance(AttendanceData attendanceData)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(attendanceData.AttendanceId.ToString()))
            {
                try
                {
                    Attendance attendance = db.Attendances.Where(x => x.AttendanceId.Equals(attendanceData.AttendanceId)).FirstOrDefault();
                    attendance.EmployeeId = attendanceData.EmployeeId;
                    attendance.ArrivingTime = attendanceData.ArrivingTime;
                    attendance.LeavingTime = attendanceData.LeavingTime;
                    attendance.Date = attendanceData.Date;
                    attendance.Status = attendanceData.Status;
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public AttendanceData GetAttendanceDataByAttendanceId(string attendanceId)
        {
            AttendanceData result = null;
            if (!string.IsNullOrEmpty(attendanceId))
            {
                try
                {
                    result = (from info in db.Attendances
                              where info.AttendanceId.ToString() == attendanceId
                              select new AttendanceData
                              {
                                  AttendanceId = info.AttendanceId,
                                  EmployeeId = info.EmployeeId,
                                  ArrivingTime = (DateTime)info.ArrivingTime,
                                  LeavingTime = (DateTime)info.LeavingTime,
                                  Date = info.Date,
                                  Status = info.Status
                              }).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        #endregion

        #region CRUD operation for AttendanceLog

        public bool CreateAttendanceLog(AttendanceLogData attendanceLogData)
        {
            bool result = false;
            if (attendanceLogData != null)
            {
                AttendanceLog attendanceLog = new AttendanceLog();
                try
                {
                    attendanceLog.LogId = Guid.NewGuid();
                    attendanceLog.AttendanceId = attendanceLogData.AttendanceId;
                    attendanceLog.EmployeeId = attendanceLogData.EmployeeId;
                    attendanceLog.TotalHours = attendanceLogData.TotalHours;
                    attendanceLog.ArriveingTime = attendanceLogData.ArrivingTime;
                    attendanceLog.LeavingTime = attendanceLogData.LeavingTime;
                    attendanceLog.Date = attendanceLogData.Date;
                    attendanceLog.Remark = attendanceLogData.Remark;
                    attendanceLog.IsActive = attendanceLogData.IsActive;

                    db.AttendanceLogs.InsertOnSubmit(attendanceLog);
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public List<AttendanceLogData> GetAttendanceLogDataList()
        {
            List<AttendanceLogData> result = null;
            try
            {
                result = (from info in db.AttendanceLogs
                          select new AttendanceLogData
                          {
                              LogId = info.LogId,
                              AttendanceId = info.AttendanceId,
                              EmployeeId = info.EmployeeId,
                              TotalHours = info.TotalHours,
                              ArrivingTime = (DateTime)info.ArriveingTime,
                              LeavingTime = (DateTime)info.LeavingTime,
                              Date = (DateTime)info.Date,
                              Remark = info.Remark,
                               IsActive=(bool)info.IsActive
                          }).ToList<AttendanceLogData>();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }
            return result;
        }

        public bool DeleteAttendanceLog(string attendanceLogId)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(attendanceLogId))
            {
                try
                {
                    var attendanceLog = db.AttendanceLogs.Where(x => x.LogId.Equals(attendanceLogId)).FirstOrDefault();
                    db.AttendanceLogs.DeleteOnSubmit(attendanceLog);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public bool UpdateAttendanceLog(AttendanceLogData attendanceLogData)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(attendanceLogData.LogId.ToString()))
            {
                try
                {
                    AttendanceLog attendanceLog = db.AttendanceLogs.Where(x => x.LogId.Equals(attendanceLogData.LogId)).FirstOrDefault();
                    attendanceLog.AttendanceId = attendanceLogData.AttendanceId;
                    attendanceLog.EmployeeId = attendanceLogData.EmployeeId;
                    attendanceLog.TotalHours = attendanceLogData.TotalHours;
                    attendanceLog.ArriveingTime = attendanceLogData.ArrivingTime;
                    attendanceLog.LeavingTime = attendanceLogData.LeavingTime;
                    attendanceLog.Date = attendanceLogData.Date;
                    attendanceLog.Remark = attendanceLogData.Remark;
                    attendanceLog.IsActive = attendanceLogData.IsActive;
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public AttendanceLogData GetAttendanceLogDataByAttendanceLogId(string attendanceLogId)
        {
            AttendanceLogData result = null;
            if (!string.IsNullOrEmpty(attendanceLogId))
            {
                try
                {
                    result = (from info in db.AttendanceLogs
                              where info.LogId.ToString() == attendanceLogId
                              select new AttendanceLogData
                              {
                                  LogId = info.LogId,
                                  AttendanceId = info.AttendanceId,
                                  EmployeeId = info.EmployeeId,
                                  TotalHours = info.TotalHours,
                                  ArrivingTime = (DateTime)info.ArriveingTime,
                                  LeavingTime = (DateTime)info.LeavingTime,
                                  Date = (DateTime)info.Date,
                                  Remark = info.Remark,
                                  IsActive =(bool)info.IsActive
                              }).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        #endregion

        #region CRUD operation for Employee

        public bool CreateEmployee(EmployeeData employeeData)
        {
            bool result = false;
            if (employeeData != null)
            {
                string empId = string.Format("E{0}", (from list in db.Employees select list).Count().ToString("0000"));

                //Guid userRoleId = new Guid("bd7f7472-2fa4-4c29-8854-973630051715"); 

                Guid userRoleId = new Guid("8bb02bd4-ed14-4750-ad84-f55f10cc6c75");
                Employee employee = new Employee();
                try
                {
                    employee.Id = Guid.NewGuid();
                    employee.FirstName = employeeData.FirstName;
                    employee.LastName = employeeData.LastName;
                    employee.TemporaryAddress = employeeData.TemporaryAddress;
                    employee.City = employeeData.City;
                    employee.CreatedDate = System.DateTime.Now;
                    employee.ContactNumber1 = employeeData.ContactNumber1;
                    employee.ContactNumber2 = employeeData.ContactNumber2;
                    employee.MiddleName = employeeData.MiddleName;
                    employee.Email = employeeData.Email;
                    employee.AlternateEmail = employeeData.Email;
                    employee.DateOfBirth = employeeData.DateOfBirth;
                    employee.Gender = employeeData.Gender;
                    employee.Image1 = employeeData.Image1;
                    employee.PermanentAddress = employeeData.PermanentAddress;
                    employee.Pincode = employeeData.Pincode;
                    employee.State = employeeData.State;
                    employee.Password = Cryptography.EncryptStringAES("welcome", "evincePassword");
                    employee.UserRoleId = userRoleId;
                    employee.EmployeeId = empId;
                    employee.IsOnline = employeeData.IsOnline;
                    employee.IsActive = 0;
                    employee.GCourse = employeeData.GCourse;
                    employee.GFrom = employeeData.GFrom;
                    employee.GTo = employeeData.GTo;
                    employee.GCourseType = employeeData.GCourseType;
                    employee.GUniversity = employeeData.GUniversity;
                    employee.GCGPA = employeeData.GCGPA;
                    employee.PCourse = employeeData.PCourse;
                    employee.PFrom = employeeData.PFrom;
                    employee.PTo = employeeData.PTo;
                    employee.PCourseType = employeeData.PCourseType;
                    employee.PUniversity = employeeData.PUniversity;
                    employee.PCGPA = employeeData.PCGPA;
                    employee.XCourse = employeeData.XCourse;
                    employee.XFrom = employeeData.XFrom;
                    employee.XTo = employeeData.XTo;
                    employee.XCourseType = employeeData.XCourseType;
                    employee.XUniversity = employeeData.XUniversity;
                    employee.XCGPA = employeeData.XCGPA;
                    employee.XIICourse = employeeData.XIICourse;
                    employee.XIIFrom = employeeData.XIIFrom;
                    employee.XIITo = employeeData.XIITo;
                    employee.XIICourseType = employeeData.XIICourseType;
                    employee.XIIUniversity = employeeData.XIIUniversity;
                    employee.XIICGPA = employeeData.XIICGPA;

                    db.Employees.InsertOnSubmit(employee);
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public List<EmployeeData> GetEmployeeDataList()
        {
            List<EmployeeData> result = null;
            try
            {
                result = (from info in db.Employees
                          join userRole in db.UserRoles on info.UserRoleId equals userRole.UserRoleId
                          select new EmployeeData
                          {
                              ID = info.Id,
                              FirstName = info.FirstName,
                              LastName = info.LastName,
                              TemporaryAddress = info.TemporaryAddress,
                              City = info.City,
                              CreatedDate = Convert.ToDateTime(info.CreatedDate),
                              ContactNumber1 = info.ContactNumber1,
                              ContactNumber2 = info.ContactNumber2,
                              MiddleName = info.MiddleName,
                              Email = info.Email,
                              AlternateEmail = info.Email,
                              DateOfBirth = info.DateOfBirth,
                              Gender = info.Gender,
                              Image1 = info.Image1,
                              PermanentAddress = info.PermanentAddress,
                              Pincode = info.Pincode,
                              State = info.State,
                              Password = info.Password,
                              UserRoleId = (Guid)info.UserRoleId,
                              EmployeeId = info.EmployeeId,
                              IsActive = Convert.ToInt32(info.IsActive),
                              GCourse = info.GCourse,
                              GFrom = info.GFrom,
                              GTo = info.GTo,
                              GCourseType = info.GCourseType,
                              GUniversity = info.GUniversity,
                              GCGPA = info.GCGPA,
                              PCourse = info.PCourse,
                              PFrom = info.PFrom,
                              PTo = info.PTo,
                              PCourseType = info.PCourseType,
                              PUniversity = info.PUniversity,
                              PCGPA = info.PCGPA,
                              XCourse = info.XCourse,
                              XFrom = info.XFrom,
                              XTo = info.XTo,
                              XCourseType = info.XCourseType,
                              XUniversity = info.XUniversity,
                              XCGPA = info.XCGPA,
                              XIICourse = info.XIICourse,
                              XIIFrom = info.XIIFrom,
                              XIITo = info.XIITo,
                              XIICourseType = info.XIICourseType,
                              XIIUniversity = info.XIIUniversity,
                              XIICGPA = info.XIICGPA,
                              UserRoleString = userRole.UserRole1,
                              IsOnline=(bool)info.IsOnline
                          }).ToList<EmployeeData>();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }
            return result;
        }

        public bool DeleteEmployee(string employeeId)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(employeeId))
            {
                try
                {
                    var employee = db.Employees.Where(x => x.Id.Equals(employeeId)).FirstOrDefault();
                    db.Employees.DeleteOnSubmit(employee);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public bool UpdateEmployee(EmployeeData employeeData)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(employeeData.ID.ToString()))
            {
                try
                {
                    Employee employee = db.Employees.Where(x => x.Id.Equals(employeeData.ID)).FirstOrDefault();
                    employee.FirstName = employeeData.FirstName;
                    employee.LastName = employeeData.LastName;
                    employee.TemporaryAddress = employeeData.TemporaryAddress;
                    employee.City = employeeData.City;
                    employee.CreatedDate = System.DateTime.Now;
                    employee.ContactNumber1 = employeeData.ContactNumber1;
                    employee.ContactNumber2 = employeeData.ContactNumber2;
                    employee.MiddleName = employeeData.MiddleName;
                    employee.Email = employeeData.Email;
                    employee.AlternateEmail = employeeData.Email;
                    employee.DateOfBirth = employeeData.DateOfBirth;
                    employee.Gender = employeeData.Gender;
                    employee.Image1 = employeeData.Image1;
                    employee.PermanentAddress = employeeData.PermanentAddress;
                    employee.Pincode = employeeData.Pincode;
                    employee.State = employeeData.State;
                    employee.Password = employeeData.Password;
                    employee.UserRoleId = employeeData.UserRoleId;
                    employee.EmployeeId = employeeData.EmployeeId;
                    employee.IsActive = employeeData.IsActive;
                    employee.GCourse = employeeData.GCourse;
                    employee.GFrom = employeeData.GFrom;
                    employee.GTo = employeeData.GTo;
                    employee.GCourseType = employeeData.GCourseType;
                    employee.GUniversity = employeeData.GUniversity;
                    employee.GCGPA = employeeData.GCGPA;
                    employee.PCourse = employeeData.PCourse;
                    employee.PFrom = employeeData.PFrom;
                    employee.PTo = employeeData.PTo;
                    employee.PCourseType = employeeData.PCourseType;
                    employee.PUniversity = employeeData.PUniversity;
                    employee.PCGPA = employeeData.PCGPA;
                    employee.XCourse = employeeData.XCourse;
                    employee.XFrom = employeeData.XFrom;
                    employee.XTo = employeeData.XTo;
                    employee.XCourseType = employeeData.XCourseType;
                    employee.XUniversity = employeeData.XUniversity;
                    employee.XCGPA = employeeData.XCGPA;
                    employee.XIICourse = employeeData.XIICourse;
                    employee.XIIFrom = employeeData.XIIFrom;
                    employee.XIITo = employeeData.XIITo;
                    employee.XIICourseType = employeeData.XIICourseType;
                    employee.XIIUniversity = employeeData.XIIUniversity;
                    employee.XIICGPA = employeeData.XIICGPA;
                    employee.IsOnline =(bool)employeeData.IsOnline;
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public EmployeeData GetEmployeeDataByEmployeeId(string employeeId)
        {
            EmployeeData result = null;
            if (!string.IsNullOrEmpty(employeeId))
            {
                try
                {
                    result = (from info in db.Employees
                              where info.Id.ToString() == employeeId
                              join userRole in db.UserRoles on info.UserRoleId equals userRole.UserRoleId
                              select new EmployeeData
                              {
                                  ID = info.Id,
                                  FirstName = info.FirstName,
                                  LastName = info.LastName,
                                  TemporaryAddress = info.TemporaryAddress,
                                  City = info.City,
                                  CreatedDate = Convert.ToDateTime(info.CreatedDate),
                                  ContactNumber1 = info.ContactNumber1,
                                  ContactNumber2 = info.ContactNumber2,
                                  MiddleName = info.MiddleName,
                                  Email = info.Email,
                                  AlternateEmail = info.Email,
                                  DateOfBirth = info.DateOfBirth,
                                  Gender = info.Gender,
                                  Image1 = info.Image1,
                                  PermanentAddress = info.PermanentAddress,
                                  Pincode = info.Pincode,
                                  State = info.State,
                                  Password = info.Password,
                                  UserRoleId = (Guid)info.UserRoleId,
                                  EmployeeId = info.EmployeeId,
                                  IsActive = Convert.ToInt32(info.IsActive),
                                  GCourse = info.GCourse,
                                  GFrom = info.GFrom,
                                  GTo = info.GTo,
                                  GCourseType = info.GCourseType,
                                  GUniversity = info.GUniversity,
                                  GCGPA = info.GCGPA,
                                  PCourse = info.PCourse,
                                  PFrom = info.PFrom,
                                  PTo = info.PTo,
                                  PCourseType = info.PCourseType,
                                  PUniversity = info.PUniversity,
                                  PCGPA = info.PCGPA,
                                  XCourse = info.XCourse,
                                  XFrom = info.XFrom,
                                  XTo = info.XTo,
                                  XCourseType = info.XCourseType,
                                  XUniversity = info.XUniversity,
                                  XCGPA = info.XCGPA,
                                  XIICourse = info.XIICourse,
                                  XIIFrom = info.XIIFrom,
                                  XIITo = info.XIITo,
                                  XIICourseType = info.XIICourseType,
                                  XIIUniversity = info.XIIUniversity,
                                  XIICGPA = info.XIICGPA,
                                  IsOnline =(bool)info.IsOnline,
                                  UserRoleString = userRole.UserRole1
                              }).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        #endregion

        #region CRUD operation for EmployeeLeaveRequest

        public bool CreateEmployeeLeaveRequest(EmployeeLeaveRequestData employeeLeaveRequestData)
        {
            bool result = false;
            if (employeeLeaveRequestData != null)
            {
                EmployeeLeaveRequest employeeLeaveRequest = new EmployeeLeaveRequest();
                try
                {
                    employeeLeaveRequest.LeaveId = Guid.NewGuid();
                    employeeLeaveRequest.EmployeeId = employeeLeaveRequestData.EmployeeId;
                    employeeLeaveRequest.TotalLeave = employeeLeaveRequestData.TotalLeave;
                    employeeLeaveRequest.LeaveReason = employeeLeaveRequestData.LeaveReason;
                    employeeLeaveRequest.FromDate = employeeLeaveRequestData.FromDate;
                    employeeLeaveRequest.ToDate = employeeLeaveRequestData.ToDate;
                    employeeLeaveRequest.LeaveType = employeeLeaveRequestData.LeaveType;
                    employeeLeaveRequest.Status = employeeLeaveRequestData.Status;
                    employeeLeaveRequest.CreatedDate = System.DateTime.Now;
                    employeeLeaveRequest.ReportTo = employeeLeaveRequestData.ReportTo;
                    employeeLeaveRequest.AssignTo = employeeLeaveRequestData.AssignTo;
                    employeeLeaveRequest.Remark = employeeLeaveRequestData.Remark;
                    employeeLeaveRequest.LeaveStatusType = employeeLeaveRequestData.LeaveStatusType;
                    employeeLeaveRequest.IsActive = employeeLeaveRequestData.IsActive;

                    db.EmployeeLeaveRequests.InsertOnSubmit(employeeLeaveRequest);
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public List<EmployeeLeaveRequestData> GetEmployeeLeaveRequestDataList()
        {
            List<EmployeeLeaveRequestData> result = null;
            try
            {
                result = (from info in db.EmployeeLeaveRequests
                          select new EmployeeLeaveRequestData
                          {
                              LeaveId = info.LeaveId,
                              EmployeeId = info.EmployeeId,
                              TotalLeave = info.TotalLeave,
                              LeaveReason = info.LeaveReason,
                              FromDate = (DateTime)info.FromDate,
                              ToDate = (DateTime)info.ToDate,
                              LeaveType = info.LeaveType,
                              Status = info.Status,
                              CreatedDate = info.CreatedDate,
                              ReportTo = info.ReportTo,
                              AssignTo = info.AssignTo,
                              Remark = info.Remark,
                              LeaveStatusType=info.LeaveStatusType,
                              IsActive=(bool)info.IsActive
                          }).ToList<EmployeeLeaveRequestData>();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }
            return result;
        }

        public bool DeleteEmployeeLeaveRequest(string leaveId)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(leaveId))
            {
                try
                {
                    var employeeLeaveRequest = db.EmployeeLeaveRequests.Where(x => x.LeaveId.Equals(leaveId)).FirstOrDefault();
                    db.EmployeeLeaveRequests.DeleteOnSubmit(employeeLeaveRequest);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public bool UpdateEmployeeLeaveRequest(EmployeeLeaveRequestData employeeLeaveRequestData)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(employeeLeaveRequestData.LeaveId.ToString()))
            {
                try
                {
                    EmployeeLeaveRequest employeeLeaveRequest = db.EmployeeLeaveRequests.Where(x => x.LeaveId.Equals(employeeLeaveRequestData.LeaveId)).FirstOrDefault();
                    employeeLeaveRequest.LeaveId = employeeLeaveRequestData.LeaveId;
                    employeeLeaveRequest.EmployeeId = employeeLeaveRequestData.EmployeeId;
                    employeeLeaveRequest.TotalLeave = employeeLeaveRequestData.TotalLeave;
                    employeeLeaveRequest.LeaveReason = employeeLeaveRequestData.LeaveReason;
                    employeeLeaveRequest.FromDate = employeeLeaveRequestData.FromDate;
                    employeeLeaveRequest.ToDate = employeeLeaveRequestData.ToDate;
                    employeeLeaveRequest.LeaveType = employeeLeaveRequestData.LeaveType;
                    employeeLeaveRequest.Status = employeeLeaveRequestData.Status;
                    employeeLeaveRequest.CreatedDate = employeeLeaveRequestData.CreatedDate;
                    employeeLeaveRequest.ReportTo = employeeLeaveRequestData.ReportTo;
                    employeeLeaveRequest.AssignTo = employeeLeaveRequestData.AssignTo;
                    employeeLeaveRequest.Remark = employeeLeaveRequestData.Remark;
                    employeeLeaveRequest.LeaveStatusType = employeeLeaveRequestData.LeaveStatusType;
                    employeeLeaveRequest.IsActive = employeeLeaveRequestData.IsActive;
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public EmployeeLeaveRequestData GetEmployeeLeaveRequestDataByLeaveId(string leaveId)
        {
            EmployeeLeaveRequestData result = null;
            if (!string.IsNullOrEmpty(leaveId))
            {
                try
                {
                    result = (from info in db.EmployeeLeaveRequests
                              where info.LeaveId.ToString() == leaveId
                              select new EmployeeLeaveRequestData
                              {
                                  LeaveId = info.LeaveId,
                                  EmployeeId = info.EmployeeId,
                                  TotalLeave = info.TotalLeave,
                                  LeaveReason = info.LeaveReason,
                                  FromDate = (DateTime)info.FromDate,
                                  ToDate = (DateTime)info.ToDate,
                                  LeaveType = info.LeaveType,
                                  Status = info.Status,
                                  CreatedDate = info.CreatedDate,
                                  ReportTo = info.ReportTo,
                                  AssignTo = info.AssignTo,
                                  Remark = info.Remark,
                                  LeaveStatusType=info.LeaveStatusType,
                                  IsActive=(bool)info.IsActive
                              }).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        #endregion

        #region CRUD OPERATION FOR Experince
        public bool CreateExperince(ExperienceData experinceData)
        {
            bool result = true;
            if (experinceData != null)
            {
                Experience experince = new Experience();
                try
                {
                    experince.ExperienceId = Guid.NewGuid();
                    experince.EmployeeId = experinceData.EmployeeId;
                    experince.EmployerName = experinceData.EmployerName;
                    experince.ContactNumber = experinceData.ContactNumber;
                    experince.Address = experinceData.Address;
                    experince.EmployeementType = experinceData.EmployeementType;
                    experince.EmploymentFrom = experinceData.EmploymentFrom;
                    experince.EmploymentTo = experinceData.EmploymentTo;
                    experince.Designation = experinceData.Designation;
                    experince.AnnualSalary = experinceData.AnnualSalary;
                    experince.Description = experinceData.Description;

                    db.Experiences.InsertOnSubmit(experince);
                    db.SubmitChanges();
                    result = true;



                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }

            }
            return result;
        }

        public bool UpdateExperince(ExperienceData experinceData)
        {
            bool result = false;
            if (experinceData != null)
            {
                var experince = db.Experiences.Where(x => x.ExperienceId.Equals(experinceData.ExperienceId)).FirstOrDefault();
                try
                {

                    experince.EmployeeId = experinceData.EmployeeId;
                    experince.EmployerName = experinceData.EmployerName;
                    experince.ContactNumber = experinceData.ContactNumber;
                    experince.Address = experinceData.Address;
                    experince.EmployeementType = experinceData.EmployeementType;
                    experince.EmploymentFrom = experinceData.EmploymentFrom;
                    experince.EmploymentTo = experinceData.EmploymentTo;
                    experince.Designation = experinceData.Designation;
                    experince.AnnualSalary = experinceData.AnnualSalary;
                    experince.Description = experinceData.Description;


                    db.SubmitChanges();
                    result = true;



                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }



            }

            return result;
        }


        public bool DeleteExperince(string experienceId)
        {
            bool result = false;
            if (experienceId != null)
            {
                try
                {

                    Experience experince = new Experience();
                    if (experienceId != null)
                    {
                        experince = db.Experiences.Where(x => x.ExperienceId.Equals(experienceId)).FirstOrDefault();
                        if (experince != null)
                        {
                            db.Experiences.DeleteOnSubmit(experince);
                            db.SubmitChanges();
                            result = true;
                        }
                    }
                    return result;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }

            }
            return result;
        }


        public ExperienceData GetExperinceDataById(string experinceId)
        {
            ExperienceData experince = new ExperienceData();
            if (experinceId != null)
            {
                Guid ExperinceId = new Guid(experinceId);

                try
                {
                    experince = (from list in db.Experiences
                                 where ExperinceId == list.ExperienceId
                                 select new ExperienceData
                                 {
                                     ExperienceId = list.ExperienceId,
                                     EmployeeId = list.EmployeeId,
                                     EmployerName = list.EmployerName,
                                     ContactNumber = list.ContactNumber,
                                     Address = list.Address,
                                     EmployeementType = list.EmployeementType,
                                     EmploymentFrom = list.EmploymentFrom,
                                     EmploymentTo = list.EmploymentTo,
                                     Designation = list.Designation,
                                     AnnualSalary = list.AnnualSalary,
                                     Description = list.Description

                                 }).FirstOrDefault();


                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }



            }
            return experince;
        }


        public List<ExperienceData> GetExperienceDataList()
        {

            List<ExperienceData> experince = new List<ExperienceData>();
            try
            {
                experince = (from list in db.Experiences
                             select new ExperienceData
                             {
                                 ExperienceId = list.ExperienceId,
                                 EmployeeId = list.EmployeeId,
                                 EmployerName = list.EmployerName,
                                 ContactNumber = list.ContactNumber,
                                 Address = list.Address,
                                 EmployeementType = list.EmployeementType,
                                 EmploymentFrom = list.EmploymentFrom,
                                 EmploymentTo = list.EmploymentTo,
                                 Designation = list.Designation,
                                 AnnualSalary = list.AnnualSalary,
                                 Description = list.Description

                             }).ToList<ExperienceData>();


            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }



            return experince;



        }


        #endregion

        #region CRUD OPERATION FOR Gallery
        public bool InsertGallery(GalleryData galleryData)
        {
            bool result = false;
            if (galleryData != null)
            {
                Gallery gallery = new Gallery();
                try
                {
                    gallery.ImageId = new Guid(galleryData.Image.Split('.')[0]);
                    gallery.Name = galleryData.Name;
                    gallery.Image = galleryData.Image;
                    gallery.IsActive = galleryData.IsActive;

                    db.Galleries.InsertOnSubmit(gallery);
                    db.SubmitChanges();
                    result = true;



                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }

            }



            return result;
        }

        public bool UpdateGallery(GalleryData galleryData)
        {
            bool result = false;
            if (galleryData != null)
            {
                Gallery gallery = db.Galleries.Where(x => x.ImageId.Equals(galleryData.ImageId)).FirstOrDefault();
                try
                {

                    gallery.Name = galleryData.Name;
                    gallery.Image = galleryData.Image;
                    gallery.IsActive = galleryData.IsActive;


                    db.SubmitChanges();
                    result = true;



                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }

            }



            return result;
        }

        public bool DeleteGallery(string imageId)
        {
            bool result = false;
            if (imageId != null)
            {
                var getImageId = db.Galleries.Where(x => x.ImageId.Equals(imageId)).FirstOrDefault();
                try
                {
                    db.Galleries.DeleteOnSubmit(getImageId);
                    db.SubmitChanges();
                    result = true;



                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }

            }



            return result;
        }

        public GalleryData GetGalleryDataById(string imageId)
        {

            Guid ImageId = new Guid(imageId);
            GalleryData galleryData = new GalleryData();
            if (ImageId != null)
            {
                try
                {
                    galleryData = (from list in db.Galleries
                                   where list.ImageId == ImageId
                                   select new GalleryData
                                   {
                                       ImageId = list.ImageId,
                                       Name = list.Name,
                                       Image = list.Image,
                                       IsActive = (int)list.IsActive
                                   }).FirstOrDefault();


                }

                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }

            }

            return galleryData;
        }


        public List<GalleryData> GetGalleryDataList()
        {

            List<GalleryData> galleryData = new List<GalleryData>();
            try
            {
                galleryData = (from list in db.Galleries
                               select new GalleryData
                               {
                                   ImageId = list.ImageId,
                                   Name = list.Name,
                                   Image = list.Image,
                                   IsActive = (int)list.IsActive
                               }).ToList<GalleryData>();


            }

            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }


            return galleryData;
        }




        #endregion

        #region CRUD OPERATION FOR PageTemplate
        public bool InsertPageTemplate(PageTemplateData pageTemplateData)
        {
            bool result = false;
            if (pageTemplateData != null)
            {
                PageTemplate pageTemplate = new PageTemplate();
                try
                {
                    pageTemplate.PageId = Guid.NewGuid();
                    pageTemplate.PageCode = pageTemplateData.PageCode;
                    pageTemplate.DisplayTitle = pageTemplateData.DisplayTitle;
                    pageTemplate.MetaKeywords = pageTemplateData.MetaKeywords;
                    pageTemplate.MetaDescription = pageTemplateData.MetaDescription;
                    pageTemplate.Contents = pageTemplateData.Contents;
                    pageTemplate.Status = pageTemplateData.Status;

                    db.PageTemplates.InsertOnSubmit(pageTemplate);
                    db.SubmitChanges();
                    result = true;


                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }


            }
            return result;
        }

        public bool UpdatePageTemplate(PageTemplateData pageTemplateData)
        {
            bool result = false;
            if (pageTemplateData != null)
            {
                PageTemplate pageTemplate = db.PageTemplates.Where(x => x.PageId.Equals(pageTemplateData.PageId)).FirstOrDefault();
                try
                {

                    pageTemplate.PageCode = pageTemplateData.PageCode;
                    pageTemplate.DisplayTitle = pageTemplateData.DisplayTitle;
                    pageTemplate.MetaKeywords = pageTemplateData.MetaKeywords;
                    pageTemplate.MetaDescription = pageTemplateData.MetaDescription;
                    pageTemplate.Contents = pageTemplateData.Contents;
                    pageTemplate.Status = pageTemplateData.Status;


                    db.SubmitChanges();
                    result = true;


                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }


            }
            return result;
        }

        public bool DeletePageTemplate(string pageId)
        {
            bool result = false;
            if (pageId != null)
            {
                var pageTemplate = db.PageTemplates.Where(x => x.PageId.Equals(pageId)).FirstOrDefault();
                try
                {

                    db.PageTemplates.DeleteOnSubmit(pageTemplate);


                    db.SubmitChanges();
                    result = true;


                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }


            }
            return result;
        }

        public PageTemplateData GetPageTemplateDataById(string pageId)
        {

            Guid PageId = new Guid(pageId);
            PageTemplateData pageTemplateData = new PageTemplateData();
            if (PageId != null)
            {
                try
                {
                    pageTemplateData = (from list in db.PageTemplates
                                        where list.PageId == PageId
                                        select new PageTemplateData
                                        {
                                            PageId = list.PageId,
                                            PageCode = list.PageCode,
                                            DisplayTitle = list.DisplayTitle,
                                            MetaKeywords = list.MetaKeywords,
                                            MetaDescription = list.MetaDescription,
                                            Contents = list.Contents,
                                            Status = (short)list.Status
                                        }).FirstOrDefault();


                }

                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }

            }

            return pageTemplateData;
        }

        public List<PageTemplateData> GetPageTemplateDataList()
        {

            List<PageTemplateData> pageTemplateData = new List<PageTemplateData>();
            try
            {
                pageTemplateData = (from list in db.PageTemplates
                                    select new PageTemplateData
                                    {
                                        PageId = list.PageId,
                                        PageCode = list.PageCode,
                                        DisplayTitle = list.DisplayTitle,
                                        MetaKeywords = list.MetaKeywords,
                                        MetaDescription = list.MetaDescription,
                                        Contents = list.Contents,
                                        Status = (short)list.Status
                                    }).ToList<PageTemplateData>();

            }

            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }



            return pageTemplateData;
        }
        #endregion

        #region CRUD OPERATION FOR UserRole

        public bool InsertUserRole(UserRoleData userRoleData)
        {
            bool result = false;
            if (userRoleData != null)
            {
                UserRole userRole = new UserRole();
                try
                {
                    userRole.UserRoleId = Guid.NewGuid();
                    userRole.UserRole1 = userRoleData.UserRole;

                    db.UserRoles.InsertOnSubmit(userRole);
                    db.SubmitChanges();
                    result = true;

                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }


            }
            return result;
        }

        public bool UpdateUserRole(UserRoleData userRoleData)
        {
            bool result = false;
            if (userRoleData != null)
            {
                UserRole userRole = db.UserRoles.Where(x => x.UserRoleId.Equals(userRoleData.UserRoleId)).FirstOrDefault();
                try
                {

                    userRole.UserRole1 = userRoleData.UserRole;


                    db.SubmitChanges();
                    result = true;

                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }


            }
            return result;
        }

        public bool DeleteUserRole(string userRoleId)
        {
            bool result = false;
            Guid UserRoleId = new Guid(userRoleId);
            if (UserRoleId != null)
            {
                var userRole = db.UserRoles.Where(x => x.UserRoleId.Equals(userRoleId)).FirstOrDefault();
                try
                {
                    db.UserRoles.DeleteOnSubmit(userRole);
                    db.SubmitChanges();
                    result = true;

                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }


            }
            return result;
        }

        public UserRoleData GetUserRoleDataById(string userRoleId)
        {

            Guid UserRoleId = new Guid(userRoleId);
            UserRoleData userRoleData = new UserRoleData();
            if (userRoleId != null)
            {
                try
                {
                    userRoleData = (from list in db.UserRoles
                                    where list.UserRoleId == UserRoleId
                                    select new UserRoleData
                                    {
                                        UserRoleId = list.UserRoleId,
                                        UserRole = list.UserRole1
                                    }).FirstOrDefault();


                }

                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }

            }

            return userRoleData;
        }




        public List<UserRoleData> GetUserRoleDataList()
        {
            List<UserRoleData> userRoleData = new List<UserRoleData>();
            try
            {
                userRoleData = (from list in db.UserRoles
                                select new UserRoleData
                                {
                                    UserRoleId = list.UserRoleId,
                                    UserRole = list.UserRole1
                                }).ToList<UserRoleData>();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }
            return userRoleData;
        }

        #endregion

        #region CRUD operation for MenuTable

        public bool CreateMenuTable(MenuTableData menuTableData)
        {
            bool result = false;
            if (menuTableData != null)
            {
                MenuTable menuTable = new MenuTable();
                try
                {
                    menuTable.MenuId = Guid.NewGuid();
                    menuTable.MenuName = menuTableData.MenuName;
                    menuTable.Title = menuTableData.Title;
                    menuTable.IsActive = Convert.ToBoolean(menuTableData.IsActive);
                    menuTable.Discription = menuTableData.Description;
                    menuTable.URL = menuTableData.URL;

                    db.MenuTables.InsertOnSubmit(menuTable);
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public List<MenuTableData> GetMenuTableDataList()
        {
            List<MenuTableData> result = null;
            try
            {
                result = (from info in db.MenuTables
                          select new MenuTableData
                          {
                              MenuId = info.MenuId,
                              MenuName = info.MenuName,
                              Title = info.Title,
                              IsActive = (bool)info.IsActive,
                              URL=info.URL,
                              Description=info.Discription
                          }).ToList<MenuTableData>();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }
            return result;
        }

        public bool DeleteMenuItem(string menuId)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(menuId))
            {
                try
                {
                    var menuTable = db.MenuTables.Where(x => x.MenuId.Equals(menuId)).FirstOrDefault();
                    db.MenuTables.DeleteOnSubmit(menuTable);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public bool UpdateMenuTable(MenuTableData menuTableData)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(menuTableData.MenuId.ToString()))
            {
                try
                {
                    MenuTable menuTable = db.MenuTables.Where(x => x.MenuId.Equals(menuTableData.MenuId)).FirstOrDefault();
                    menuTable.MenuName = menuTableData.MenuName;
                    menuTable.Title = menuTableData.Title;
                    menuTable.IsActive = Convert.ToBoolean(menuTableData.IsActive);
                    menuTable.Discription = menuTableData.Description;
                    menuTable.URL = menuTableData.URL;
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public MenuTableData GetMenuTableDataByParamId(string menuId)
        {
            MenuTableData result = null;
            if (!string.IsNullOrEmpty(menuId))
            {
                try
                {
                    result = (from info in db.MenuTables
                              where info.MenuId.ToString() == menuId
                              select new MenuTableData
                              {
                                  MenuId = info.MenuId,
                                  MenuName = info.MenuName,
                                  Title = info.Title,
                                  IsActive = (bool)info.IsActive,
                                  URL=info.URL,
                                  Description=info.Discription
                              }).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        #endregion

        #region CRUD operation for RoleMenuManager

        public bool CreateRoleMenuManager(RoleMenuManagerData roleMenuManagerData)
        {
            bool result = false;
            if (roleMenuManagerData != null)
            {
                RoleMenuManager roleMenuManager = new RoleMenuManager();
                try
                {
                    roleMenuManager.RoleMenuManagerId = Guid.NewGuid();
                    roleMenuManager.RoleId = roleMenuManagerData.RoleId;
                    roleMenuManager.MenuId = roleMenuManagerData.MenuId;
                    roleMenuManager.ParentId = roleMenuManagerData.ParentId;
                    roleMenuManager.SortOrder = roleMenuManagerData.SortOrder;

                    db.RoleMenuManagers.InsertOnSubmit(roleMenuManager);
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }


        public List<RoleMenuManagerData> GetRoleMenuManagerDataList()
        {
            List<RoleMenuManagerData> result = null;
            try
            {
                result = (from info in db.RoleMenuManagers
                          select new RoleMenuManagerData
                          {
                              RoleMenuManagerId = info.RoleMenuManagerId,
                              MenuId = info.MenuId,
                              RoleId = info.RoleId,
                              SortOrder = (Int32)info.SortOrder,
                              ParentId = info.ParentId
                          }).ToList<RoleMenuManagerData>();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }
            return result;
        }

        public bool DeleteRoleMenuManager(string roleMenuManagerId)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(roleMenuManagerId))
            {
                try
                {
                    var roleMenuManager = db.RoleMenuManagers.Where(x => x.RoleMenuManagerId.Equals(roleMenuManagerId)).FirstOrDefault();
                    db.RoleMenuManagers.DeleteOnSubmit(roleMenuManager);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public bool UpdateRoleMenuManager(RoleMenuManagerData roleMenuManagerData)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(roleMenuManagerData.MenuId.ToString()))
            {
                try
                {
                    RoleMenuManager roleMenuManager = db.RoleMenuManagers.Where(x => x.RoleMenuManagerId.Equals(roleMenuManagerData.MenuId)).FirstOrDefault();
                    roleMenuManager.RoleId = roleMenuManagerData.RoleId;
                    roleMenuManager.MenuId = roleMenuManagerData.MenuId;
                    roleMenuManager.ParentId = roleMenuManagerData.ParentId;
                    roleMenuManager.SortOrder = roleMenuManagerData.SortOrder;
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }
        public RoleMenuManagerData GetRoleMenuManagerDataByParamId(string roleMenuManagerId)
        {
            RoleMenuManagerData result = null;
            if (!string.IsNullOrEmpty(roleMenuManagerId))
            {
                try
                {
                    result = (from info in db.RoleMenuManagers
                              where info.RoleMenuManagerId.ToString() == roleMenuManagerId
                              select new RoleMenuManagerData
                              {
                                  MenuId = info.MenuId,
                                  RoleId = info.RoleId,
                                  ParentId=info.ParentId,
                                  SortOrder = (Int32)info.SortOrder,
                                  RoleMenuManagerId = info.RoleMenuManagerId
                              }).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        #endregion

        #region CRUD Opreration For LeaveMaster
        public bool CreateLeave(LeaveMasterData leaveMasterData)
        {
            bool result = false;
            if (leaveMasterData != null)
            {
                LeaveMaster leaveMaster = new LeaveMaster();
                try
                {
                    leaveMaster.LeaveMasterId = Guid.NewGuid();
                    leaveMaster.SickLeave = leaveMasterData.Sickleave;
                    leaveMaster.PrivilageLeave = leaveMasterData.PrivilageLeave;
                    leaveMaster.CasualLeave = leaveMasterData.CasualLeave;
                    leaveMaster.LeaveWithoutPay = leaveMasterData.LeaveWithoutPay;
                    leaveMaster.Year = leaveMasterData.Year;
                    leaveMaster.RoleId = leaveMasterData.RoleId;
                    db.LeaveMasters.InsertOnSubmit(leaveMaster);
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        
        public List<LeaveMasterData> GetLeaveMasterDataList()
        {
            List<LeaveMasterData> result = null;
            try
            {
                result = (from info in db.LeaveMasters
                          select new LeaveMasterData
                          {
                              LeaveMasterId = info.LeaveMasterId,
                              Sickleave = (decimal)info.SickLeave,
                              PrivilageLeave = (decimal)info.PrivilageLeave,
                              CasualLeave = (decimal)info.CasualLeave,
                              LeaveWithoutPay = (decimal)info.LeaveWithoutPay,
                              RoleId=info.RoleId,
                              Year = (DateTime)info.Year
                          }).ToList<LeaveMasterData>();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }
            return result;
        }

        public bool DeleteLeaveMaster(string leaveMasterId)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(leaveMasterId))
            {
                try
                {
                    var leaveMaster = db.LeaveMasters.Where(x => x.LeaveMasterId.Equals(leaveMasterId)).FirstOrDefault();
                    db.LeaveMasters.DeleteOnSubmit(leaveMaster);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public bool UpdateLeaveMaster(LeaveMasterData leaveMasterData)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(leaveMasterData.LeaveMasterId.ToString()))
            {
                try
                {
                    LeaveMaster leaveMaster = db.LeaveMasters.Where(x => x.LeaveMasterId.Equals(leaveMasterData.LeaveMasterId)).FirstOrDefault();
                    
                    leaveMaster.SickLeave = leaveMasterData.Sickleave;
                    leaveMaster.PrivilageLeave = leaveMasterData.PrivilageLeave;
                    leaveMaster.CasualLeave = leaveMasterData.CasualLeave;
                    leaveMaster.LeaveWithoutPay = leaveMasterData.LeaveWithoutPay;
                    leaveMaster.Year = leaveMasterData.Year;
                    leaveMaster.RoleId = leaveMasterData.RoleId;
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }
        public LeaveMasterData GetLeaveMasterDataByParamId(string leaveMasterId)
        {
            LeaveMasterData result = null;
            if (!string.IsNullOrEmpty(leaveMasterId))
            {
                try
                {
                    result = (from info in db.LeaveMasters
                              where info.LeaveMasterId.ToString() == leaveMasterId
                              select new LeaveMasterData
                              {
                                  LeaveMasterId = info.LeaveMasterId,
                                  Sickleave = (decimal)info.SickLeave,
                                  PrivilageLeave = (decimal)info.PrivilageLeave,
                                  CasualLeave = (decimal)info.CasualLeave,
                                  LeaveWithoutPay = (decimal)info.LeaveWithoutPay,
                                  Year = (DateTime)info.Year,
                                  RoleId=info.RoleId
                              }).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        #endregion

        #region CRUD Operation For EmployeeLeaveMaster
        public bool CreateEmployeeLeave(EmployeeLeaveMasterData employeeLeaveMasterData)
        {
            bool result = false;
            if (employeeLeaveMasterData != null)
            {
                EmployeeLeaveMaster employeeLeaveMaster = new EmployeeLeaveMaster();
                try
                {
                    employeeLeaveMaster.EmployeeLeaveMasterId = Guid.NewGuid();
                    employeeLeaveMaster.EmployeeId = employeeLeaveMasterData.EmployeeId;
                    employeeLeaveMaster.SickLeave = employeeLeaveMasterData.Sickleave;
                    employeeLeaveMaster.PrevilageLeave = employeeLeaveMasterData.PrivilageLeave;
                    employeeLeaveMaster.CasualLeave = employeeLeaveMasterData.CasualLeave;
                    employeeLeaveMaster.LeaveWithoutPay = employeeLeaveMasterData.LeaveWithoutPay;
                    employeeLeaveMaster.Year = employeeLeaveMasterData.Year;
                    db.EmployeeLeaveMasters.InsertOnSubmit(employeeLeaveMaster);
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }


        public List<EmployeeLeaveMasterData> GetEmployeeLeaveMasterDataList()
        {
            List<EmployeeLeaveMasterData> result = null;
            try
            {
                result = (from info in db.EmployeeLeaveMasters
                          select new EmployeeLeaveMasterData
                          {
                              EmployeeLeaveMasterId = info.EmployeeLeaveMasterId,
                              EmployeeId = (Guid)info.EmployeeId,
                              Sickleave = (decimal)info.SickLeave,
                              PrivilageLeave = (decimal)info.PrevilageLeave,
                              CasualLeave = (decimal)info.CasualLeave,
                              LeaveWithoutPay = (decimal)info.LeaveWithoutPay,
                              Year =(DateTime) info.Year
                          }).ToList<EmployeeLeaveMasterData>();
            }
            catch (Exception ex)
            {
                LogEngine.Log(ex.Message);
                throw ex;
            }
            return result;
        }

        public bool DeleteEmployeeLeaveMaster(string employeeLeaveMasterId)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(employeeLeaveMasterId))
            {
                try
                {
                    var leaveMaster = db.EmployeeLeaveMasters.Where(x => x.EmployeeLeaveMasterId.Equals(employeeLeaveMasterId)).FirstOrDefault();
                    db.EmployeeLeaveMasters.DeleteOnSubmit(leaveMaster);
                    db.SubmitChanges();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }

        public bool UpdateEmployeeLeaveMaster(EmployeeLeaveMasterData employeeLeaveMasterData)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(employeeLeaveMasterData.EmployeeLeaveMasterId.ToString()))
            {
                try
                {
                    EmployeeLeaveMaster leaveMaster = db.EmployeeLeaveMasters.Where(x => x.EmployeeLeaveMasterId.Equals(employeeLeaveMasterData.EmployeeLeaveMasterId)).FirstOrDefault();
                    
                    leaveMaster.EmployeeId = employeeLeaveMasterData.EmployeeId;
                    leaveMaster.SickLeave = employeeLeaveMasterData.Sickleave;
                    leaveMaster.PrevilageLeave = employeeLeaveMasterData.PrivilageLeave;
                    leaveMaster.CasualLeave = employeeLeaveMasterData.CasualLeave;
                    leaveMaster.LeaveWithoutPay = employeeLeaveMasterData.LeaveWithoutPay;
                    leaveMaster.Year = employeeLeaveMasterData.Year;
                    db.SubmitChanges();
                    result = true;
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }
        public EmployeeLeaveMasterData GetEmployeeLeaveMasterDataByParamId(string employeeLeaveMasterId)
        {
            EmployeeLeaveMasterData result = null;
            if (!string.IsNullOrEmpty(employeeLeaveMasterId))
            {
                try
                {
                    result = (from info in db.EmployeeLeaveMasters
                              where info.EmployeeLeaveMasterId.ToString() == employeeLeaveMasterId
                              select new EmployeeLeaveMasterData
                              {
                                  EmployeeLeaveMasterId = info.EmployeeLeaveMasterId,
                                  EmployeeId = (Guid)info.EmployeeId,
                                  Sickleave = (decimal)info.SickLeave,
                                  PrivilageLeave = (decimal)info.PrevilageLeave,
                                  CasualLeave = (decimal)info.CasualLeave,
                                  LeaveWithoutPay = (decimal)info.LeaveWithoutPay,
                                  Year =(DateTime) info.Year
                              }).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    LogEngine.Log(ex.Message);
                    throw ex;
                }
            }
            return result;
        }
        #endregion

        public void Dispose()
        {
            throw new NotImplementedException();
        }
       

    }
}
